package com.qding.park.manager.timer.schedule.job;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobExecutionContext;
import org.quartz.JobKey;
import org.springframework.stereotype.Service;

import com.baidu.disconf.client.common.annotations.DisconfUpdateService;
import com.baidu.disconf.client.common.update.IDisconfUpdate;
import com.qding.park.common.core.cache.service.CacheServiceFacade;
import com.qding.park.common.core.constant.CacheKeyThree;
import com.qding.park.common.core.redis.SerializeUtils;
import com.qding.park.common.utils.DateUtils;
import com.qding.park.manager.pojo.TbInvitecard;
import com.qding.park.manager.pojo.TbMonthRent;
import com.qding.park.manager.pojo.TbReserve;
import com.qding.park.manager.pojo.queue.QueueMsg;
import com.qding.park.manager.pojo.queue.QueueTypeEnum;
import com.qding.park.manager.timer.ParkTimerApplication;
import com.qding.park.manager.timer.conf.TaskConfig;

/**
 * @ClassName ReserveCheckJob
 * @Description TODO(预定单超时检查任务:分为两种超时 1超时2延期超时)
 * @author Administrator
 * @Date 2016年9月15日 下午3:53:58
 * @version 1.0.0
 */
@Service
@DisconfUpdateService( classes={TaskConfig.class},itemKeys = { "task.clearTimeOutOrderJob.name" ,"task.clearTimeOutOrderJob.taskJobClass","task.clearTimeOutOrderJob.cronTime"})
public class ClearTimeOutOrderJob extends AbstractJob implements IDisconfUpdate{
	private static final Log _log = LogFactory.getLog(ClearTimeOutOrderJob.class);
//
//	// 订单失效时间 以分钟为单位
//	private static final int timeout_minute = 30;

	@Override
	public void execJob(JobExecutionContext context) {
		JobKey jobKey = context.getJobDetail().getKey();
		
		CacheServiceFacade cacheServiceFacade = ParkTimerApplication.ctxUtil.getApplicationContext()
				.getBean(CacheServiceFacade.class);

//		// 清除临停订单缓存数据
//		List<TbReserve> reserveList = cacheServiceFacade.tempPay_cache_getAll();
//		for (TbReserve tbReserve : reserveList) {
//
//			if (DateUtils.addMinute(tbReserve.getCreate_date(), timeout_minute).getTime() < new Date().getTime()) {
//				// 过期
//				cacheServiceFacade.tempPay_cache_delete(tbReserve);
//			}
//		}
//
//		List<TbMonthRent> monthRentList = cacheServiceFacade.monthrentPay_cache_getAll();
//		for (TbMonthRent tbMonthRent : monthRentList) {
//
//			if (DateUtils.addMinute(tbMonthRent.getCreate_date(), timeout_minute).getTime() < new Date().getTime()) {
//				// 过期
//				cacheServiceFacade.monthrentPay_cache_delete(tbMonthRent.getId());
//			}
//		}
		
		QueueMsg  msg=new QueueMsg();
		msg.setEventType(QueueTypeEnum.CLEAR_TIMEOUT_ORDER.getType());
		
		String msgStr=SerializeUtils.serialize(msg);
		
		cacheServiceFacade.msg_queue_push(msg);
		_log.info("清除缴费单过期数据: " + jobKey + " executing at " + new Date());

	}

	@Override
	public void reload() throws Exception {
		// TODO Auto-generated method stub
		_log.info(TaskConfig.getTask_clearTimeOutOrderJob_name());
		String taskName=TaskConfig.getTask_clearTimeOutOrderJob_name();
		String cronTime=TaskConfig.getTask_clearTimeOutOrderJob_cronTime();
		super.reloadTask(taskName,cronTime);
	}

}
